<?php

/**
 * 导入类
 */
use EasyWechat\Kernel\Http\StreamResponse;

/**
 * 微信小程序二维码操作类
 */
class WechatMiniQrcode extends MiniBase {


    /**
     * 适用于需要的码数量较少的业务场景
     * 
     * $page string 小程序路由
     * $path string 小程序码保存路径
     * $set array 小程序码的配置
     * $set = [
     *   "width" => ''  二维码宽度
     *   "auto_color" => false 默认false
     *   "line_color" => auto_color是生效使用rgb设置颜色
     * ]
     * 
     */
    function getLessMiniQrcode($page,$path,$fileName = null,array $set = []){
        $response = null;
        if(empty($set)){
            $response = $this->app->app_code->get($page);
        }else{
            $response = $this->app->app_code->get($page,$set);
        }

        if($response instanceof StreamResponse) {
            $filename = '';
            if($fileName) {
                $filename = $response->save($path);
            } else {
                $filename = $response->saveAs($path,$fileName);
            }
            return ["status" => true,"filename" =>$filename,'msg' => 'success'];
        }else{
            return ["status" => false,"filename" =>'','msg' => "请检查参数是否正确"];
        }
    }


    /**
     * 适用于需要的码数量极多，或仅临时使用的业务场景 个数不受限 永久有效
     * 
     *
     * $path string 小程序码保存路径
     * $set array 小程序码的配置
     * $set = [
     *   "page" => ""  要跳转的路由
     *   "width" => ''  二维码宽度
     *   "auto_color" => false 默认false
     *   "line_color" => auto_color是生效使用rgb设置颜色
     * ]
     * 
     */
    function getUnlimit($path,$fileName = null,array $set) {
        if(!isset($set['page'])){
            return ["status" => false ,"msg" => '$set 中必须包含page 配置（要跳转的路由）',"filename" => ''];
        }
        $response = $this->app->app_code->getUnlimit('scene-value',$set);

        if($response instanceof StreamResponse) {
            $filename = '';
            if($fileName) {
                $filename = $response->save($path);
            } else {
                $filename = $response->saveAs($path,$fileName);
            }
            return ["status" => true,"filename" =>$filename,'msg' => 'success'];
        }else {
            return ["status" => false,"filename" =>'','msg' => "请检查参数是否正确"];
        }

    }

    /**
     * 适用于需要的码数量较少的业务场景 个数受限 永久有效
     * 
     * $page string 小程序路由
     * $path string 小程序码保存路径
     * $fileName string  小程序码名称   
     * $set array 小程序码的配置
     * "width" => ''  二维码宽度
     *   
     * 
     */
    function getQrcode($page,$path,$fileName=null,$width = null) {

        $response = $this->app->app_code->getQrCode($page,$width);

        if($response instanceof StreamResponse) {
            $filename = '';
            if($fileName) {
                $filename = $response->save($path);
            } else {
                $filename = $response->saveAs($path,$fileName);
            }
            return ["status" => true,"filename" =>$filename,'msg' => 'success'];
        }else {
            return ["status" => false,"filename" =>'','msg' => "请检查参数是否正确"];
        }
    }


}
